<?php

class AppController extends Controller {
	public $components = array('session','acl','auth','captcha');
	public $my = null;	
	public function beforeFilter() {
        //Configure AuthComponent
        $this->C_auth->authorize = 'actions';
        $this->C_auth->loginAction = '/admin/default/login';
        $this->C_auth->logoutRedirect = '/admin/default/login';
        $this->C_auth->loginRedirect = '/admin/default/index';
		$this->C_auth->ajaxLogin = '/admin/default/login';
		$this->C_auth->allowedActions = array('display');
/*		$this->C_auth->actionPath = 'controllers/';
  		$this->_lockSiteURL(Param::read('siteURL')); */		
		$this->_getMyInfo(); 
    }

  	public function _getMyInfo() {
	    $myifo = array();
		if ($this->C_auth->user()) {
			$myID = $this->C_auth->user('id');			
			$sql ='[@@:UM user][@@:QU select][_select user.* ][_from][_table user:yes][_where {%_where%}][_limit 1]';			
			$myInfo = POP::exe($sql,array('user.id' => $myID));		
			$myifo = $myInfo[0]['user'];
			foreach ($myInfo[0]['user'] as $key => $value) {
			    if($key=='password')continue;
				$this->my->$key = $value;
			}
		} else {
			$this->my = null;
		}
		$this->set('myifo',$myifo);
	} 	
	
	public function fpage($tb = array()) {
        $tb = $tb + array('model' => '', 'joins' => array(),'fields' => array('*'),
					'conditions' => '',	'order' => '',
					'limit' => '', 'group' => '','url' => '',
					'page' => 1,'plist' => 6,'startPage' => true					
					) ; 		
        extract($tb, EXTR_SKIP);
 		$data = array();
		if($startPage) {

            $page = empty($page) ? 1 : intval($page);
			$plist = empty($plist) ? 6 : intval($plist);
			$query = array(
					'conditions' => $conditions, 
					'fields' => array(' COUNT(*) AS count '), 
					'joins' => $joins, 
					);	
			$data1 = POP::exe('[@@:UM '.$model.'][@@:QU find:first]',$query);
		    $totalnum = $data1[0]['count'];		
	        $totalpage=intval(@ceil($totalnum/$limit));
	        $nextpage=$page+1;
	        $uppage=$page-1;		 
	        if($nextpage>$totalpage){
		        $nextpage=$totalpage;
	        }		 
	        if($uppage<1){
		        $uppage=1;
	        }
	        $s=$page-3;
	        if($s<1){
		       $s=1;
	        }
	        $b=$s + $plist;
	        if($b>$totalpage){
		        $b=$totalpage;
	        }			
	        for($j=$s;$j<=$b;$j++){
		        if($j==$page) {
			        $data['page']['current']['num'] = $j;
					$data['page']['current']['url'] = "$url&page=$j";
		        } else {      
			        $data['page']['plist'][$j] = "$url&page=$j";			         
		        }
	        }		  
            if(!isset($data['page']['current'])){
		        $data['page']['current']['num'] =1;
		        $data['page']['current']['url'] = "$url&page=1"; 
		    }	  
		    $start = ($page-1) * $limit;		  	  
		    $data['page']['head'] =  "$url&page=1";
		    $data['page']['prevPage'] = "$url&page=$uppage";
		    $data['page']['nextPage'] = "$url&page=$nextpage";
		    $data['page']['foot'] = "$url&page=$totalpage";
		    $data['page']['pageCount'] = $totalpage;
		    $data['page']['total'] = $totalnum;
		}else {
		    $start = 0;
		}
		if($start) $limit = " {$start},{$limit} ";
		$query = array(
				'conditions' => $conditions, 
				'fields' => $fields, 
				'joins' => $joins, 
				'limit' => $limit,
				'offset' => $start, 
				'order' => $order, 
				'group' => $group
				);	
	    $data['data'] = POP::exe('[@@:UM '.$model.'][@@:QU find:all]',$query);	
        return $data ;
	}	
}
?>